package com.weimei.conversionTreasure.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;

import android.os.Bundle;

import com.google.android.material.tabs.TabLayout;
import com.weimei.conversionTreasure.R;
import com.weimei.conversionTreasure.app.AppViewModelFactory;
import com.weimei.conversionTreasure.databinding.ActivityTotalBinding;
import com.weimei.conversionTreasure.viewModel.KhRecordVM;
import com.weimei.conversionTreasure.viewModel.TotalVM;

import java.util.Calendar;

import me.goldze.mvvmhabit.base.BaseActivity;
import me.tatarka.bindingcollectionadapter2.BR;

public class TotalActivity extends BaseActivity<ActivityTotalBinding, TotalVM> {


    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_total;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.setActivity(this);
        viewModel.setPieView(binding.totalPie);

        binding.khTab.addTab(binding.khTab.newTab().setText("月度"));
        binding.khTab.addTab(binding.khTab.newTab().setText("年度"));
        binding.khTab.getTabAt(0).select();

        binding.khTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewModel.type.set(""+tab.getPosition());

                if (tab.getPosition() == 0){
                    Calendar calendar = Calendar.getInstance();
                    int year = calendar.get(Calendar.YEAR);
                    int month = calendar.get(Calendar.MONTH)+1;
                    viewModel.time.set(year+"-"+month);
                }else {
                    Calendar calendar = Calendar.getInstance();
                    int year = calendar.get(Calendar.YEAR);
                    viewModel.time.set(year+"");
                }

                viewModel.onResume();



            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public TotalVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, (ViewModelProvider.Factory) factory).get(TotalVM.class);
    }
}